using System;
using System.Collections.Generic;
using System.Text;
using Presentation.Contracts;
using Task.Contracts;
using DataAccess;
using Globals;
using Task;
using System.Data;

namespace Presentation
	{
	public sealed class SearchForumsPresenter
		{
		#region Members

		/// <summary>
		/// Instance of view.
		/// </summary>
		private ISearchForumsView view;

		/// <summary>
		/// Simple galleryTask.
		/// </summary>
		private IForumTask task;

		#endregion Members

		#region Construction/Finalization

		/// <summary>
		/// Initializes a new instance of the <see cref="AddForumPresenter"/> class.
		/// </summary>
		/// <param name="view">The view.</param>
		public SearchForumsPresenter(ISearchForumsView view)
			{
			this.view = view;
			task = new ForumTask();
			}

		#endregion Construction/Finalization

		#region Public Methods

		/// <summary>
		/// Initializes view.
		/// </summary>
		public void Initialize()
			{
			
			}

		/// <summary>
		/// Adds the new forum.
		/// </summary>
		public void SearchForums()
			{
			List<DataAccess.forum> tmpList = task.SearchForums(view.SearchedString);
			DataTable dt = new DataTable();
			dt.Columns.Add("name", typeof(string));
			dt.Columns.Add("description", typeof(string));
			dt.Columns.Add("owner",typeof(string));
			dt.Columns.Add("id", typeof(string));
			foreach (DataAccess.forum f in tmpList)
				{
				DataRow dr = dt.NewRow();
				dr[0] = f.name + " (" + task.CountTopicsInForum(f.id).ToString() + ")";
				if(f.description!=null)
					dr[1] = f.description.ToString();
				if (f.user != null) 
					dr[2] = f.user.login.ToString();
				dr[3] = f.id.ToString();
				dt.Rows.Add(dr);
				}
			view.ShowSearchedForums(dt);
			}

		public void SignMeToForum(int forum_id)
			{
			task.SignToForum(view.LoggedUserID, forum_id);
			}

		#endregion Public Methods

		#region Private Methods

		/// <summary>
		/// Gets the forum object from view.
		/// </summary>
		/// <returns></returns>
		private forum GetForumObjectFromView()
			{
			forum forumTmp = new forum();
			/*
			forumTmp.description = view.ForumDescription;
			forumTmp.name = view.ForumName;
			*/
			//tmp user id - nie tak, tak nie dziala wlasnie, trzea podac obiekt a nie id obiektu
			//w zasadzie nigdy sie nie dowiedzialem dlaczego... ale tak niestety jest czyli: zobacz w Tasku!
			//forumTmp.user_id = ContentNamesGlobals.TmpUserId;


			return forumTmp;
			}

		#endregion Private Methods
		}
	}